<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/7/11
 * Time: 下午3:56
 */
class AdjustController extends BaseController{
    public function order_check(){
        $validator = Validator::make(Input::all(),
            array(
                'order_id' => 'required',
                'date' => 'required',
                'shop' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
            $order_id = Input::get('order_id');
            $date = Input::get('date');
            $shop = Input::get('shop');

            //验证token
           // $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
           // if(is_numeric($car_data) && $car_data < 0){
           //     $response = Lang::get("ods_error.token_check_error_12005");
           //     break;
           // }
            $order = Orders::where('order_id',$order_id)->first();
            if(!is_null($order)){
                if(date('Y-m-d',strtotime($order->pay_time)) != $date){
                    $response = array('code' => -1, 'msg' => '日期不正确。', 'result' => array());
                    break;
                }
                if($order->shop_id != $shop){
                    $response = array('code' => -1, 'msg' => '门店不正确。', 'result' => array());
                    break;
                }
                $response = array('code' => 0, 'msg' => '查询成功。', 'result' => array('order_id' => $order->order_id,'amount' => $order->payable_amount));
            }else{
                $response = array('code' => -1, 'msg' => '订单不存在。', 'result' => array());
                break;
            }

        } while (false);
        Log::info("info", array("system" => "adjust" ,"event" => "order_check", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    public function order_check_all(){
        $validator = Validator::make(Input::all(),
            array(
                'order_id' => 'required',
                'date' => 'required',
                'shop' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
            $order_id = json_decode(Input::get('order_id'));
            $date = Input::get('date');
            $shop = Input::get('shop');

            //验证token
           // $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
           // if(is_numeric($car_data) && $car_data < 0){
           //     $response = Lang::get("ods_error.token_check_error_12005");
           //     break;
           // }
            $orders = Orders::whereNotIn('order_id',$order_id)
                ->where('shop_id',$shop)
                ->whereBetween('pay_time',array($date . ' 00:00:00',$date . ' 23:59:59'))
                ->select(array('order_id'))
                ->get()
                ->toArray();
            $count = Orders::where('shop_id',$shop)
                ->whereBetween('pay_time',array($date . ' 00:00:00',$date . ' 23:59:59'))
                ->whereIn('status',array(1018,1017,1217,1317,1417))
                ->count();
            $sum = Orders::where('shop_id',$shop)
                ->whereBetween('pay_time',array($date . ' 00:00:00',$date . ' 23:59:59'))
                ->whereIn('status',array(1018,1017,1217,1317,1417))
                ->sum('payable_amount');
            $response = array(
                'code' => 0,
                'msg' => '查询成功。',
                'result' => array(
                    'order_id' => $orders,
                    'count' => $count,
                    'amount' => $sum,
                )
            );
        } while (false);
        Log::info("info", array("system" => "adjust" ,"event" => "order_check_all", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-05-03 17:16:23
     * @Description: 提交车牌的驾驶证信息
     */
    public function report_dirver(){
        $validator = Validator::make(Input::all(),
            array(
                'car_no' => 'required',
            )
        );

        $response = 0;
        do {
            if ($validator->fails()) {
                $response = -1;
                break;
            }

            //传入参数
            $car_no = Input::get('car_no');
            $car_type = Input::get('car_type');
            $own = Input::get('own');
            $address = Input::get('address');
            $model = Input::get('model');
            $vin = Input::get('vin');
            $engine_number = Input::get('engine_number');
            $register_time = Input::get('register_time');
            $send_time = Input::get('send_time');
            $use_nature = Input::get('use_nature');
            $img = Input::get('img');
            $shop_id = Input::get('shop_id');
            $dl=DrivingLicense::where("car_no",$car_no)->first();
            if(is_null($dl)){
                $dl=new DrivingLicense();                
            }
            $dl->car_no = $car_no;
            $dl->car_type =$car_type;
            $dl->own = $own;
            $dl->address = $address;
            $dl->model = $model;
            $dl->vin =$vin;
            $dl->engine_number = $engine_number;
            $dl->register_time = $register_time;
            $dl->send_time = $send_time;
            $dl->use_nature = $use_nature;
            $dl->img = $img;
            $dl->shop_id = $shop_id;
            $dl->updated_at = Common::currentTimeString();
            $dl->save();

           
        } while (false);
        Log::info("info", array("system" => "adjust" ,"event" => "report_dirver", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }
    /**
     * @Author:      lyt
     * @DateTime:  2017-05-04 09:29:46
     * @Description: 提交行驶证的图片
     */
    public function report_dirverimg(){
        $validator = Validator::make(Input::all(),
            array(
                'car_no' => 'required',
            )
        );

        $response = 0;
        do {
            if ($validator->fails()) {
                $response = -1;
                break;
            }
             $pic = Input::get('pic')?Input::get('pic'):null; //环检图
             $car_no = Input::get('car_no');

            if($pic){
                $pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.png';
                $img = base64_decode($pic);
                $path=Config::get('config.driving_license_file');
                File::put( $path . $pic_name, $img);
                $order_ring_check['pic'] = $pic_name;
                $dl=DrivingLicense::where("car_no",$car_no)->first();
                if($dl){ //存在
                    $dl->img=$pic_name;
                    $dl->save();
                }else{ //不存在
                    $dl=new DrivingLicense;
                    $dl->img=$pic_name;
                    $dl->save();
                }
            }           
        } while (false);
        Log::info("info", array("system" => "adjust" ,"event" => "report_dirver", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }



    /**
     * @Author:      lyt
     * @DateTime:  2017-08-31 13:40:54
     * @Description: 提交 扫描车牌
     */
    public function scanning_car_no(){
        $response = 0;
        do {
            $input = file_get_contents('php://input');
            $input = iconv("gb2312","utf-8//IGNORE",$input);

            // $data = iconv("gb2312","utf-8//IGNORE",$data);
            $data = json_decode($input,true);
            if(isset($data['AlarmInfoPlate']['result']['PlateResult']['license'])){
                $deviceName = $data['AlarmInfoPlate']['deviceName'];            
                $ipaddr = $data['AlarmInfoPlate']['ipaddr'];
                $license = $data['AlarmInfoPlate']['result']['PlateResult']['license'];    
                $path=Config::get('wentong.scanning_file'); 
                $big_pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.jpg';     
                $smail_pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.jpg';     
                //如果有半小时内的同样车牌照的 不重复记录
                $flp=ScanningLicensePlates::where("scanning_plate",$license)->where("collecting_shop",$deviceName)->orderBy("created_at","desc")->first();  
                if($flp&&(time()-strtotime($flp->created_at)-Config::get('wentong.time_up')*60)<0){
                    return $response;
                }

                //保存大图
                $imageFileLen = $data['AlarmInfoPlate']['result']['PlateResult']['imageFileLen'];
                if($imageFileLen > 0){
                    $imageFile = $data['AlarmInfoPlate']['result']['PlateResult']['imageFile']; 
                    file_put_contents($path . $big_pic_name , base64_decode($imageFile));            }
                //保存小图
                $imageFragmentFileLen = $data['AlarmInfoPlate']['result']['PlateResult']['imageFragmentFileLen'];
                if($imageFragmentFileLen > 0){
                    $imageFragmentFile = $data['AlarmInfoPlate']['result']['PlateResult']['imageFragmentFile'];                           
                    file_put_contents($path . $smail_pic_name , base64_decode($imageFragmentFile));
                }   
                $slp=new ScanningLicensePlates();
                $slp->scanning_plate=$license;
                $slp->collecting_shop=$deviceName;
                $slp->big_picture=$big_pic_name;
                $slp->small_picture=$smail_pic_name;
                $slp->shop_status=0;
                $slp->save();         
                $result = array(
                    'code' => 1,
                    'type' => 2,        
                    'shop_id' => $deviceName,         
                    'order_id' => "",
                    'plate_car_no' =>$license,
                    'plate_id' =>$slp->id,
                ); 
                RedisHelper::push($result,"anhui_wash_panel");       
            }           
        } while (false);
        Log::info("info", array("system" => "adjust" ,"event" => "scanning_car_no", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }
}